package model;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class CalcList implements List<CalcItem> {

	private HashMap<String, CalcItem> internalList = new HashMap<String, CalcItem>();

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.List#add(java.lang.Object)
	 */
	@Override
	public boolean add(CalcItem object) {
		return (internalList.put(object.termId, object) == null);
	}

	@Override
	public void add(int location, CalcItem object) {
		add(object); // TODO ?
	}

	@Override
	public boolean addAll(Collection<? extends CalcItem> arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean addAll(int arg0, Collection<? extends CalcItem> arg1) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void clear() {
		internalList.clear();
	}

	@Override
	public boolean contains(Object object) {
		if (!(object instanceof CalcItem))
			return false;
		CalcItem x = (CalcItem) object;
		if (internalList.get(x.termId) != null)
			return true;
		return false;
	}

	@Override
	public boolean containsAll(Collection<?> arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public CalcItem get(int location) {
		Object[] internalListAsArray = internalList.values().toArray();
		if ((internalListAsArray.length < location + 1) || (location < 0)) {
			return null;
		}
		return (CalcItem) internalListAsArray[location];
	}

	@Override
	public int indexOf(Object object) {
		// TODO
		return 0;
	}

	@Override
	public boolean isEmpty() {
		return internalList.isEmpty();
	}

	@Override
	public Iterator<CalcItem> iterator() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int lastIndexOf(Object object) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public ListIterator<CalcItem> listIterator() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ListIterator<CalcItem> listIterator(int location) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public CalcItem remove(int location) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean remove(Object object) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean removeAll(Collection<?> arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean retainAll(Collection<?> arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public CalcItem set(int location, CalcItem object) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int size() {
		return internalList.size();
	}

	@Override
	public List<CalcItem> subList(int start, int end) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object[] toArray() {
		return internalList.values().toArray();
	}

	@Override
	public <CalcItem> CalcItem[] toArray(CalcItem[] array) {
		// TODO Auto-generated method stub
		return (CalcItem[]) internalList.values().toArray();
	}

	public CalcItem findItemById(String id) {
		return internalList.get(id);
	}

	public CalcItem addItem(String term, String descr, String theme) {
		String id = StringAnalyser.getIdInString(term);
		String original = StringAnalyser.getTermInString(term);
		CalcItem newCalcItem = new CalcItem(id, original, descr, theme);
		internalList.put(id, newCalcItem);
		return newCalcItem;
	}

}
